<?php
/**
 * 功能: 签到
 * User: WD
 * Date: 2019/1/8 0008
 * Time: 15:48
 */

namespace app\index\controller;

use think\Controller;
use think\Db;

class Sign extends Controller{
    /**
     * 签到规则
     */
    public function rule(){
        $res = Db::name('sign_rule')->where('id',1)->field('image,intro')->find();

        return json(['msg'=>'请求成功','ok'=>true,'data'=>$res]);
    }

    /**
     * 签到
     */
    public function index(){
        $token =  input('token');
        $user_id = model('User')->getUserId($token);
        if(empty($user_id)){
            return json(['msg'=>'您还未登录，或者登录被占用！','ok'=>false,'code'=>501]);
        }
        $check = Db::name('sign')->where('user_id',$user_id)->order('time desc')->find();

        $interval_day = (strtotime(date('Y-m-d',time()))-strtotime(date('Y-m-d',$check['time'])))/(3600*24);//间隔天数

        if($interval_day==0){
            return json(['msg' => '你今天已经签过到了', 'ok' => false]);
        }else{
            $data = ['user_id'=>$user_id,'time'=>time()];

            // 启动事务
            Db::startTrans();
            try{
                $integral =  Db::name('sign_rule')->where('id',1)->value('integral');
                Db::name('user')->where('id',$user_id)->setInc('integral',$integral);
                Db::name('sign')->insert($data);
                // 提交事务
                Db::commit();
                return json(['msg' => '签到成功', 'ok' => true]);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['msg' => $e->getMessage(), 'ok' => false]);
            }
        }

    }
    /**
     * 用户可兑换金额
     */
    public function amount(){
        $token =  input('token');
        if(empty($token)){
            return json(['msg'=>'token不能为空！','ok'=>false]);
        }
        $user = model('User')->getUser($token);
        if(empty($user)){
            return json(['msg'=>'您还未登录，或者登录被占用！','ok'=>false,'code'=>501]);

        }else{
            $sign_rule = Db::name('sign_rule')->find();
            $amount    = intval($user['integral']*($sign_rule['money']/$sign_rule['score']));

            $data = [
                'integral'=>$user['integral'],//积分总数
                'amount'=>$amount//可兑换金额
            ];
            return json(['msg'=>'请求成功！','ok'=>true,'data'=>$data]);
        }

    }

    /**
     * 积分兑换
     */
    public function exchange(){
        $token     =  input('token');
        $user      = model('User')->getUser($token);
        $user_id   = $user['id'];
        $amount    = intval(input('amount'));

        $sign_rule = Db::name('sign_rule')->find();
        $integral  = $amount*$sign_rule['score'];

        if(empty($user)||empty($token)){
            return json(['msg'=>'您还未登录，或者登录被占用！','ok'=>false,'code'=>501]);
        }
        if($amount<=0||$user['integral']<$sign_rule['score']){
            return json(['msg'=>'您没有达到兑换标准！','ok'=>false,'code'=>501]);
        }
        // 启动事务
        Db::startTrans();
        try {
            Db::table('user')->where('id',$user_id)->setDec('integral',$integral);
            Db::table('user')->where('id',$user_id)->setInc('commission',$amount);
            // 提交事务
            Db::commit();
            return json(['msg'=>'兑换成功！','ok'=>true]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['msg'=>$e->getMessage(),'ok'=>false]);
        }


    }

}